<template>
  <div class='headers'>
    <Table :modelData="modelData" :listData="listData" :pageInfo="pageInfo" :handleSelectionChange="handleSelectionChange"></Table>
  </div>
</template>
<script>
import Table from "../../components/table";
export default {
  components: { Table },
  name: "",
  data() {
    return {
      modelData: [
        { myType: "select",},
        { myType: "index", label: "序号" },
        { myType: "normal", prop: "content", label: "题目" },
        {
          myType: "operate",
          label: "管理",
          btns: [
            {
              label: "编辑",
              click: this.edit,
              icon: require("../../assets/images/manageHome/edit.png")
            },
            {
              label: "删除",
              click: this.delete,
              icon: require("../../assets/images/manageIssues/delete.png")
            }
          ],
          width: "180"
        }
      ],
      listData: [
        // {
        //   img:
        //     "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3014768788,2397820870&fm=27&gp=0.jpg"
        // },
      ],
      pageInfo: {
        page: 1,
        total: 10,
        size: 10,
        changePage: this.changePage
      }
    };
  },
  methods: {
    handleSelectionChange(selection){
      var goal = selection.map(val=>{
        return {
          id:val.id
        }
      })
      this.$store.state.delete.ids=goal
    },
    edit(scope) {
      this.$store.state.add.show = true;
      this.$store.state.add.info=JSON.parse(JSON.stringify(scope.row))
      this.$store.state.add.OK=this.saveEdit
    },
    saveEdit(){
      var params=JSON.parse(JSON.stringify(this.$store.state.add.info))
      delete params.createTimeStr
      delete params.state
      _g.apiPost('answer/save.do',params).then(res=>{
        _g.toMessage(res)
        if(!res.error){
          this.getListData()
        }
      })
    },
    // 删除
    delete(scope) {
      this.$store.state.confirmInfo.id=scope.row.id
      this.$store.state.confirmInfo.message = "是否确认删除？";
      this.$store.state.confirmInfo.show = true;
      this.$store.state.confirmInfo.OK=this.sureDelete
    },
    sureDelete(){
      var params={
        data:this.$store.state.confirmInfo.id
      }
      _g.apiPost('answer/deleteVos.do',params).then(res=>{
        _g.toMessage(res)
        if(!res.error){
          this.getListData()
        }
      })
    },

    // 翻页
    changePage() {this.getListData()},
    getListData(){
      var params={
        currPageNo:this.pageInfo.page
      }
      _g.apiPost('answer/init.do',params).then(res=>{
        this.pageInfo.total=res.data.total
        this.$set(this,'listData',res.data.list)
      })
    }
  },
  created() {
    this.getListData()
    _bus.$on('flashM',()=>{
      this.getListData()
    })
  },
  beforeDestroy(){
    _bus.$off('flashM')
  }
};
</script>
<style lang="scss" scoped>
.headers {
  height: 100%;
  flex-grow: 1;
}
</style>